//
//  NearWeiboViewController.m
//  01-HZ88微博-架构的搭建
//
//  Created by kangkathy on 16/5/23.
//  Copyright © 2016年 kangkathy. All rights reserved.
//

#import "NearWeiboViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "WeiboAnnotation.h"
#import "DataService.h"
#import "WeiboModel.h"
#import "YYModel.h"
#import "WeiboAnnotationView.h"

@interface NearWeiboViewController () <MKMapViewDelegate> {
    CLLocationManager *_locationManager;
}

@end

@implementation NearWeiboViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _locationManager = [[CLLocationManager alloc] init];
    
    
    if (kiOS8Later) {
        [_locationManager requestWhenInUseAuthorization];
    }
    
    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    
    [self.view addSubview:mapView];
    
    mapView.delegate = self;
    

    mapView.showsUserLocation = YES;
    

    
    
}

#pragma mark - MKMapViewDelegate 
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    
    NSLog(@"用户位置已经更新");
    

    CLLocationCoordinate2D coordinate = userLocation.coordinate;
    MKCoordinateSpan span = MKCoordinateSpanMake(0.05, 0.05);
    
    
    [mapView setRegion:MKCoordinateRegionMake(coordinate, span) animated:YES];
    
    NSString *lat = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *lon = [NSString stringWithFormat:@"%f", coordinate.longitude];
    
    
    //请求附近的微博数据
    [DataService requestWithURL:@"place/nearby_timeline.json" params:@{@"lat" : lat, @"long" : lon} fileData:nil httpMethod:@"get" success:^(NSURLSessionDataTask *task, id result) {
        
        //NSLog(@"%@", result);
        NSArray *statuses = result[@"statuses"];
        
        for (NSDictionary *status in statuses) {
            
            WeiboModel *model = [WeiboModel yy_modelWithDictionary:status];
            
            WeiboAnnotation *annotation = [[WeiboAnnotation alloc] init];
            
            annotation.weibo = model;
            
            //            NSLog(@"%@", model);
            [mapView addAnnotation:annotation];
            
            
        }
        
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
        
    }];

}

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    
    //    static NSString *identity = @"annotation";
    //    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identity];
    //    if (pinView == nil) {
    //
    //        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identity];
    //    }
    //
    //    //从天而降而降的动画效果
    //    pinView.animatesDrop = YES;
    //
    //
    //
    //    return pinView;
    
    
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        
        return nil;
        
    }
    
    
    static NSString *identity = @"annotation";
    
    WeiboAnnotationView *weiboView = (WeiboAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identity];
    
    if (weiboView == nil) {
        weiboView = [[WeiboAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identity];
    }
    
    
    return weiboView;
    
    
    
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray<MKAnnotationView *> *)views {
    for (UIView *view in views) {
        view.alpha = 0;
        
        view.transform = CGAffineTransformMakeScale(0.3, 0.3);
        [UIView animateWithDuration:0.4 animations:^{
            view.alpha = 1;
            
            view.transform = CGAffineTransformMakeScale(1.4, 1.4);
        } completion:^(BOOL finished) {
            
            [UIView animateWithDuration:0.4 animations:^{
                view.transform = CGAffineTransformIdentity;
                 }];
            }];
        
        
    }
}

@end
